package com.Druid1;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.jdbc.core.JdbcTemplate;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.pool.DruidDataSource;

@SpringBootApplication
public class Application {
	@Autowired
	DruidDataSource dataSource;

	@Bean
	public RedisTemplate<String, Object> redisTemplate(
			RedisConnectionFactory factory) {
		RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
		redisTemplate.setConnectionFactory(factory);
		redisTemplate.setKeySerializer(new StringRedisSerializer(Charset
				.forName("utf8")));
	
		FastJson2JsonRedisSerializer<Object> serializer = new FastJson2JsonRedisSerializer<>(
				Object.class);
		redisTemplate.setValueSerializer(serializer);
		return redisTemplate;
	}

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

	@Bean
	public DruidDataSource dataSource(@Autowired MyDataSourceFilter filter) {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setUrl("jdbc:mysql://192.168.1.50:3306/test");
		dataSource.setPassword("root");
		dataSource.setUsername("root");
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		List<Filter> filters = new ArrayList<>();
		filters.add(filter);
		//Druid	数据源添加过滤器
		dataSource.setProxyFilters(filters);
		return dataSource;
	}

	@Bean
	public JdbcTemplate jdbcTemplate(@Autowired DataSource dataSource) {
		return new JdbcTemplate(dataSource);
	}

}
